package com.lyk.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_timeRules;
import com.common.entity.PageBean;
import com.lyk.dao.A_timeRulesDao;

public class A_timeRulesDaoImpl extends BaseDao<A_timeRules> implements A_timeRulesDao {

	@Override
	public A_timeRules findById(Integer n) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_timeRules where timeRules_id = ? ");
		paramList.add(n);
		List<A_timeRules> list = findBySql(sf.toString(), paramList.toArray());
		return (list!=null&&list.size()>0)?list.get(0):null;
	}

	@Override
	public int changetimeRules(A_timeRules timeRules) {
		String sql = "update a_timeRules set timeLimit=? where timeRules_id = ?";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(timeRules.getTimeLimit());
		paramList.add(timeRules.getTimeRules_id());
		return this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public List<A_timeRules> findByCondition(Map<String, Object> mp) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_timeRules where 1=1 ");
		if(mp!=null && mp.size()>0){
				if(mp.get("timeRules_id")!=null){
					sf.append("and timeRules_id = ? ");
					paramList.add(mp.get("timeRules_id"));
				}
		}
		return findBySql(sf.toString(), paramList.toArray());
	}
	public A_timeRules getEntity(ResultSet rs){
		try {
			A_timeRules timeRules = new A_timeRules();
			timeRules.setTimeRules_id(rs.getInt("timeRules_id"));
			timeRules.setTimeLimit(rs.getInt("timeLimit"));
			return timeRules;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int delA_timeRules(Integer n) {
		String sql = "delete A_timeRules where timeRules_id = ?";
		return this.executeUpdate(sql, new Object[]{n});
	}

	@Override
	public int savetimeRules(A_timeRules timeRules) {
		String sql="insert into A_timeRules(timeRules_id,timeLimit) values(seq_a_timeRules.nextval,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(timeRules.getTimeLimit());
		return this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public PageBean<A_timeRules> findPageByCondition(int pageIndex,
			int pageSize, Map<String, Object> mp) {
		PageBean<A_timeRules> pageBean=new PageBean<A_timeRules>();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from a_timeRules where 1=1");
		if(mp!=null && mp.size()>0){
			if(mp.get("timeRules_id")!=null){
				querySql.append("and timeRules_id = ? ");
				paramList.add(mp.get("timeRules_id"));
			}
		}
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		if(pageBean.getTotalCount()>0){
			List<A_timeRules> list = findPage(pageBean, paramList, querySql);
			pageBean.setList(list);
		}
		
		return pageBean;
	}
	
	private List<A_timeRules> findPage(PageBean<A_timeRules> pageBean,
			List<Object> paramList, StringBuffer querySql) {
		List<A_timeRules> list=new ArrayList<A_timeRules>();
		StringBuffer queryPageSql=new StringBuffer();
		queryPageSql.append(" select * from (");
		queryPageSql.append("  select tmp.*,rownum rn from ( ");
		queryPageSql.append(querySql.toString());
		queryPageSql.append("  ) tmp ");
		queryPageSql.append(" ) where rn>? and rn<=? ");
		int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
		int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
		paramList.add(startRow);
		paramList.add(endRow);
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			conn=this.getConnection();
			prs=conn.prepareStatement(queryPageSql.toString());
			Object[] objs=paramList.toArray();
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			rs=prs.executeQuery();
			
			while(rs.next()){
				A_timeRules timeRules = new A_timeRules();
				timeRules.setTimeRules_id(rs.getInt("timeRules_id"));
				timeRules.setTimeLimit(rs.getInt("timeLimit"));
				list.add(timeRules);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
